import { queryPipesList, addPipes, removePipes, getPipes, editPipes,rollbackPipes } from '@/services/municipal';

export default {
  namespace: 'pipes',
  state: {
    pipesList: [],
    pipesdata: {},
    data: {},
  },

  effects: {
    *fetchPipes({ payload }, { call, put }) {
      const response = yield call(queryPipesList, payload);

      if (!response.ErrorMessage) {
        yield put({
          type: 'queryPipes',
          payload: response.Data.pipes,
        });
      } else {
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *addPipes({ payload, callback }, { call, put }) {
      const response = yield call(addPipes, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback(response);
    },

    *getPipes({ payload, callback }, { call, put }) {
      const response = yield call(getPipes, payload);
      yield put({
        type: 'savePipes',
        payload: response,
      });
      if (callback) callback(response);
    },

    *editPipes({ payload, callback }, { call, put }) {
      const response = yield call(editPipes, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback(response);
    },

    *removePipes({ payload, callback }, { call, put }) {
      const response = yield call(removePipes, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback(response);
    },

    *rollbackPipes({ payload, callback }, { call, put }) {
      const response = yield call(rollbackPipes, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    queryPipes(state, action) {
      return {
        ...state,
        pipesList: action.payload,
      };
    },
    savePipes(state, action) {
      return {
        ...state,
        pipesdata: action.payload,
      };
    },
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
  },
};
